/*
 * MonomialIdeal.java
 *
 * Created on May 6, 2005, 11:59 AM
 *
 * To change this template, choose Tools | Options and locate the template under
 * the Source Creation and Management node. Right-click the template and choose
 * Open. You can then make changes to the template in the Source Editor.
 */

package com.milowski.monos.monomial;

import java.util.*;

import com.milowski.monos.*;

import org.infoset.xml.InfosetFactory;
import org.infoset.xml.ItemConstructor;
import org.infoset.xml.ItemDestination;
import org.infoset.xml.XMLException;

/**
 *
 * @author R. Alexander Milowski
 */
public class MonomialIdeal extends LexOrderedList implements XMLMarshaller
{
   
   /** Creates a new instance of MonomialIdeal */
   public MonomialIdeal(LexOrder order)
   {
      super(order);
   }
   
   public boolean add(Object o) {
      if (!(o instanceof Monomial)) {
         throw new ClassCastException("Only Monomial instances are allowed in a monomial ideal: "+o.getClass().getName());
      }
      return super.add(o);
   }
   
   public void add(int index,Object o) {
      if (!(o instanceof Monomial)) {
         throw new ClassCastException("Only Monomial instances are allowed in a monomial ideal: "+o.getClass().getName());
      }
      super.add(index,o);
   }
   
   private void checkCollection(Collection c) {
      for (Iterator toCheck = c.iterator(); toCheck.hasNext(); ) {
         Object o = toCheck.next();
         if (!(o instanceof Monomial)) {
            throw new ClassCastException("Only Monomial instances are allowed in a monomial ideal: "+o.getClass().getName());
         }
      }
   }
   
   public boolean addAll(Collection c) {
      checkCollection(c);
      return super.addAll(c);
   }
   
   public boolean addAll(int index,Collection c) {
      checkCollection(c);
      return super.addAll(index,c);
   }
   
   public Object set(int index,Object o) {
      if (!(o instanceof Monomial)) {
         throw new ClassCastException("Only Monomial instances are allowed in a monomial ideal: "+o.getClass().getName());
      }
      return super.set(index, o);
   }
   
   public String toString() {
      return toString(null);
   }
   
   public String toString(LexOrder inScopeOrder) {
      StringBuffer sb = new StringBuffer();
      if (inScopeOrder!=order) {
         sb.append("(monomial-ideal-with-order ");
         sb.append(order.toString());
      } else {
         sb.append("(monomial-ideal");
      }
      for (Iterator toOutput=iterator(); toOutput.hasNext();) {
         Monomial m = (Monomial)toOutput.next();
         sb.append(' ');
         sb.append(m.format(order));
      }              
      sb.append(")");
      return sb.toString();
   }
   
   public void toXML(ItemDestination dest) throws XMLException {
      toXML(InfosetFactory.getDefaultInfoset().createItemConstructor(),dest);
   }

   public void toXML(ItemConstructor constructor, ItemDestination dest) throws XMLException {
      dest.send(constructor.createElement(XML.monomialIdealName));
      dest.send(constructor.createCharacters("\n"));
      order.toXML(constructor,dest);
      dest.send(constructor.createCharacters("\n"));
      for (Iterator monomials=iterator(); monomials.hasNext(); ) {
         Monomial m = (Monomial)monomials.next();
         m.toXML(constructor,dest);
         dest.send(constructor.createCharacters("\n"));
      }
      dest.send(constructor.createElementEnd(XML.monomialIdealName));
      dest.send(constructor.createCharacters("\n"));
   }

}
